<template>
  <div>
    <!--工具栏-->
    <div class="head-container" style="margin-top: 5px">
      <crudOperation />
    </div>
    <!--表格渲染-->
    <el-table border stripe ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
              @filter-change="crud.handleFilterChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column v-if="columns.visible('crtTs')" prop="crtTs"
                       :show-overflow-tooltip="true"
                       label="升级时间" min-width="140">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.crtTs }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('oldVersion')" prop="oldVersion"
                       min-width="150px" label="升级前版本"/>
      <el-table-column v-if="columns.visible('newVersion')" prop="newVersion"
                       min-width="150px" label="升级后版本"/>
      <el-table-column v-if="columns.visible('crtUser')" :show-overflow-tooltip="true"
                       prop="crtUser" label="操作人"/>
      <el-table-column v-if="columns.visible('status')" :show-overflow-tooltip="true" label="状态"
                       prop="status"
                       key="status"
                       :filters="[{ text: '成功', value: 'SUCCESS' }, { text: '失败', value: 'FAIL' }]"
                       :filter-method="filterAgentStatus"
                       filter-placement="bottom-end" :filter-multiple="false" column-key="status">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.status=='SUCCESS'" align="center"
                  type="success"
                  style="width:60px;">成功
          </el-tag>
          <el-tag size="small" v-if="scope.row.status=='FAIL'" align="center" type="error"
                  style="width:60px;">失败
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('restore')" width="120" prop="restore"
                       label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-refresh-left" :disabled="scope.row.status === 'FAIL'" @click="toRestore(scope.row)">
            恢复
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import crudOperation from '@crud/extend/components/CRUD.OnlyLookOperation'
import remoteAgentApi from '@/api/agent/remoteAgent'

const defaultForm = {}
const defaultCrud = CRUD({
  title: '调度服务升级日志',
  url: '/api/agentUpgradeLog/selectPages',
  sort: 'sort,asc'
})
export default {
  name: 'upgradeLog',
  components: {crudOperation},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  props: {
    agentInfo: Object,
    default: null
  },
  data() {
    return {
      runLogDialogVisible: false,
      runLogInfo: null,
      appType: 'hdrs-agent'
    }
  },
  computed: {},
  created() {

  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params = {agentCode: this.agentInfo.agentCode}
    },
    filterAgentStatus(value, row) {
      return row.status === value;
    },
    toRestore(data) {
      this.$confirm(`确认【恢复】到${data.oldVersion}版本吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let dataMap = {
          restoreParam: data.backUpPath
        }
        let dataForm = {
          appType: this.appType,
          agentCode: data.agentCode,
          agentName: data.agentName,
          agentHost: data.agentIp,
          version: data.oldVersion,
          dataMap: dataMap
        }
        const loading = this.$loading({
          lock: true,
          text: '拼命恢复中......',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        remoteAgentApi.restore(dataForm).then(res => {
          if ("200" === res.code) {
            this.crud.notify(data.agentName + '恢复成功！', CRUD.NOTIFICATION_TYPE.SUCCESS);
          } else {
            this.crud.notify(data.agentName + '恢复失败！失败原因：' + res.msg, CRUD.NOTIFICATION_TYPE.ERROR);
          }
          loading.close();
        }).catch(() => {
          loading.close();
        })
      }).catch(() => {
      })
    },
  }
}
</script>
